package com.zr.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.springframework.stereotype.Service;

import com.zr.dao.BaseDao;
import com.zr.dao.impl.BaseDaOImpl;
import com.zr.model.Role;
import com.zr.model.State;
import com.zr.model.User;
import com.zr.service.AddRoleService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Service("addRoleService")
public class AddRoleServiceImpl<T> extends BaseDaOImpl<T> implements AddRoleService {
	@Resource
	private BaseDao<User>  baseDao;
	@Override
	public Role addRole(String rname, String str2) {
		Role role=new Role();
		role.setRname(rname);
		role.setFuncs(str2);
		this.getCurrentSession().save(role);
		return role;
	}
	@Override
	public JSONArray findRole() {
		JSONArray j= new JSONArray();
		String hql = " from Role ";
		Query query = this.getCurrentSession().createQuery(hql);
		List <Role>  fs = query.list();
		for (Role func : fs) {
			JSONObject j1 = new JSONObject();
			j1.put("id", func.getRid());
			j1.put("text",func.getRname());	
			j.add(j1);		
		}
			return j;
	}
	@Override
	public JSONArray findUser() {
		JSONArray j= new JSONArray();
		String hql = " from User ";
		Query query = this.getCurrentSession().createQuery(hql);
		List <User>  fs = query.list();
		for (User func : fs) {
			JSONObject j1 = new JSONObject();
			j1.put("id", func.getUid());
			j1.put("text",func.getUname());	
			j.add(j1);	
	}
       return j;
}
	@Override
	public boolean updateRole(int UserName, int UserRole) {
		boolean flg=false;
		String sql =  "UPDATE `user` SET rid=:rid WHERE uid=:uid";
    	Query  query =  this.getCurrentSession().createSQLQuery(sql);
    	query.setInteger("rid", UserRole);
    	query.setInteger("uid",UserName );
    	int i  = query.executeUpdate();
    	if(i>0){
    		flg=true;
    	}
		return flg;
	}
}